package com.ykdl.member.kid.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 有关预产期计算的算法
 * 
 * @author 大师♂罗莊
 * 
 */
public class PregnancyHelp {
	private String[] predate_shuzu;

	/**
	 * @param args
	 * @throws ParseException
	 */

	public String getPredate(String date, int day_max) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date d1 = sdf.parse(date);// "2014-2-20"
		Calendar d = Calendar.getInstance();
		d.setTime(d1);
		return setValue(d, day_max);
	}

	/**
	 * @param localCalendar1
	 *            末次月经时间
	 * @throws ParseException
	 */
	public static String setValue(Calendar localCalendar1, int day_max)
			throws ParseException {

		// 取今天时间
		Calendar Calendartoday = Calendar.getInstance();
		// 克隆一个保存下
		Calendar Calendarborn = (Calendar) localCalendar1.clone();

		// 取今天和末次月经时间相差多少天
		long daysBetweennumber = daysBetween(localCalendar1.getTime(),
				Calendartoday.getTime());

		// 算出多少周
		int WeekBetweennumber = (int) daysBetweennumber / 7;
		// 零多少天
		long i1 = (daysBetweennumber + 1) % 7;

		Calendarborn.add(Calendar.DAY_OF_MONTH, 280 + day_max);
		long borndayBetween = -1
				+ daysBetween(Calendartoday.getTime(), Calendarborn.getTime());
		int bornyear = Calendarborn.get(Calendar.YEAR);
		int bornmonth = 1 + Calendarborn.get(Calendar.MONTH);
		int bornday = Calendarborn.get(Calendar.DAY_OF_MONTH);
		String current = (WeekBetweennumber + "周" + i1 + "天");
		getMediMonthSpace(WeekBetweennumber);
		String born = (borndayBetween + "天");
		String borndayString = (String.valueOf(bornyear) + "年"
				+ String.valueOf(bornmonth) + "月" + String.valueOf(bornday) + "日");
		return bornyear + "," + bornmonth + "," + bornday;
	}

	/**
	 * 计算方法：从末次月经第一天算起，月份减3，如不够时则加9，日数加7。
	 * 
	 * @param cal
	 *            从末次月经
	 */
	static Calendar Calendarbornmanual(Calendar cal, int day_max) {
		Calendar temp = (Calendar) cal.clone();
		int month1 = temp.get(Calendar.MONTH) + 1;
		if (month1 < 3) {
			temp.add(Calendar.MONTH, 9);
		} else {
			temp.add(Calendar.MONTH, -3);
			temp.add(Calendar.YEAR, 1);
		}
		temp.add(Calendar.DAY_OF_MONTH, 7 + day_max);
		outprint(temp);
		return temp;
	}

	/**
	 * 计算两个日期之间相差的天数 需要用calendar，因为Date返回的永远是UTC时间，也就是时区永远比我们晚8个小时。
	 * 
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static long daysBetween(Date smdate, Date bdate)
			throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);
		return between_days;
	}

	static void outprint(Calendar cal) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	}

	/**
	 * 有人问我32w+（32周）怀孕几个月
	 * 
	 * @param week
	 *            输入周数
	 */
	public static void getMediMonthSpace(int week) {
		// 此算法同原昆明某三甲医院产科主任算法一致，所以为什么我的算法和她医生算出来的一致
		int month = week * 7 / 30;// 周数算出月数，可以按28天或者30天算
		int weekleave = week * 7 % 30;// 周数算出剩下的天数
		// 也可以通过/7算出周数，大家自己去补充了
	}

	/**
	 * 算两个日期差几个月，不能用Calendar.MONTH直接相减，否则2012年11月和2013年11月就是0
	 * 
	 * @param scalendar
	 *            日期1
	 * @param calendar
	 *            日期2
	 * @return 两个日期差几个月
	 */
	public static int getMonthSpace(Calendar scalendar, Calendar calendar) {

		int year1 = calendar.get(Calendar.YEAR);
		int year2 = scalendar.get(Calendar.YEAR);
		int month1 = calendar.get(Calendar.MONTH);
		int month2 = scalendar.get(Calendar.MONTH);

		int count = 0;
		if (year1 == year2 && month1 > month2) {
			count = month1 - month2;
		} else if (year1 == year2 && month1 < month2) {
			count = month2 - month1;
		} else if (year1 > year2 && month1 > month2) {
			count = (year1 - year2) * 12 + (month1 - month2);
		} else if (year1 > year2 && month1 < month2) {
			count = (year1 - year1) * 12 + (month2 - month1);
		} else if (year1 < year2 && month1 > month2) {
			count = (year2 - year1) * 12 + month1 - month2;
		} else if (year1 < year2 && month1 < month2) {
			count = (year2 - year1) * 12 + month2 - month1;
		}
		if (count < 0) {

			count = Math.abs(count);
		}
		return count;
	}

}
